/**
 * ---------------------------
 * Time: 2017/8/6 23:14.
 * Author: Cangshi
 * View: http://palerock.cn
 * ---------------------------
 */
define(
    ['app', '../../device-dao'],
    function (app, DeviceDao) {
        app.factory('deviceDao', function (INDEXED_DB, PromiseUtil, Device, ObjectUtils, ArrayUtils) {

            // init
            var deviceDao = INDEXED_DB.deviceDao = new DeviceDao();
            var dexieDB = INDEXED_DB.dexieDB;
            var devices = dexieDB.devices;

            var checkDevice = function (device, cb) {
                if (!Device.isDevice(device)) {
                    if (!cb || !(cb instanceof Function)) {
                        return false;
                    }
                    cb.call(window, null, {
                        code: 403,
                        message: '欲添加的对象不是一个设备'
                    });
                    return false;
                }
                ObjectUtils.ergodicObject(this, device, function (value, key) {
                    if (value == null) {
                        delete device[key];
                    }
                });
                return true;
            };

            /**
             * 添加一台设备
             * @param device
             * @param cb {function=}
             */
            deviceDao.addDevice = function (device, cb) {
                if (!checkDevice(device, cb)) {
                    return;
                }
                var promise = devices.add(device);
                PromiseUtil.parserCallBack(promise, cb, '添加设备[no=\'' + device.deviceNo + '\']');
            };

            /**
             * 更新一台设备
             * @param device
             * @param cb
             */
            deviceDao.updateDeviceByDeviceNoSelective = function (device, cb) {
                if (!checkDevice(device, cb)) {
                    return;
                }
                var promise = devices.put(device);
                PromiseUtil.parserCallBack(promise, cb, '更新设备[no=\'' + device.deviceNo + '\']')
            };


            /**
             * 查询所有的设备
             * @param cb
             * @param sort
             */
            deviceDao.findAllDevices = function (cb, sort) {
                if (sort == null) {
                    sort = ':id'
                }
                var promise = devices.orderBy(sort).toArray();
                PromiseUtil.parserCallBack(promise, cb, '查询所有设备');
            };


            /**
             * 通过设备编号获取指定设备信息
             * @param deviceNo
             * @param cb
             */
            deviceDao.findDeviceByDeviceNo = function (deviceNo, cb) {
                var promise = devices.where('deviceNo').equals(deviceNo).toArray();
                PromiseUtil.parserCallBack(promise, cb, '获取[deviceNo = \'' + deviceNo + '\']设备');
            };


            /**
             * 通过设备编号删除一台设备
             * @param deviceNo
             * @param cb
             */
            deviceDao.deleteDeviceByDeviceNo = function (deviceNo, cb) {
                var promise = devices.delete(deviceNo);
                PromiseUtil.parserCallBack(promise, cb, '删除[deviceNo = \'' + deviceNo + '\']设备');
            };

            return deviceDao;
        })
    }
);